﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WSDZ.KM.ZH.XiaoManIntegrating40
{
   public static class XiaoManManager
    {
        public static string GetValueByKey(this string key,JArray array,string group_id)
        {
            try
            {
                if (array==null|| array.Count<1)
                {
                    return "";
                }
                string res = array.Where(x => x["group_id"].ToStringV1().EqualsV1(group_id)).Select(x =>
                  {
                      JArray fieldArray = JsonConvert.DeserializeObject<JArray>(x["fields"].ToStringV1());
                      string ywy = fieldArray.Where(a => a["id"].ToStringV1().EqualsV1(key)).Select(a => a["value"].ToStringV1()).FirstOrDefault();
                      return ywy;
                  }).FirstOrDefault();
                return res.ToStringV1();
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static string GetValueByKey(this string key, List<XMFieldData> xmdatas)
        {
            try
            {
                if (xmdatas == null || xmdatas.Count < 1)
                    return "";
                XMFieldData obj = xmdatas.Where(x => x.id.EqualsV1(key)).FirstOrDefault();
                return obj == null ? "" : obj.value;
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}
